package com.shujia.chuantong;


import java.io.*;
import java.util.HashMap;

/*
    不借助分而治之的思想完成需求：
    使用IO流，map集合完成
    key是clazz, value值是班级的人数

    HashMap<String,Integer>
 */
public class CountClazzNumDemo {
    public static void main(String[] args) throws Exception {
        //创建文件输入流对象
        BufferedReader br = new BufferedReader(new FileReader("hadoop/data/students.csv"));

        //创建Map集合
        HashMap<String, Integer> map = new HashMap<>();

        String line = null;
        while ((line=br.readLine())!=null){
            String clazz = line.split(",")[4];
            if(!map.containsKey(clazz)){
                map.put(clazz,1);
            }else {
                map.put(clazz,map.get(clazz)+1);
            }
        }

        //创建文件输出流对象
        BufferedWriter bw = new BufferedWriter(new FileWriter("hadoop/data/out.txt"));
        map.forEach((k,v)->{
            try {
                bw.write(k+" "+v);
                bw.newLine();
                bw.flush();
            } catch (IOException e) {
                e.printStackTrace();
            }
        });

        bw.close();
        br.close();

    }
}
